17-3 ��Ʈw�d�ߡG���p�ʸ�ƪ�

¸ê®Æ®w¥i¥H¥]§t¼Æ­Ó¸ê®Æªí¡A¤£¦P¸ê®Æªí¤§¶¡ªºÄæ¦ì¥i¥H¦³ÃöÁp©m¡A§Ú­Ì¥i¥H®Ú¾Ú³o¨Ç¸ê®ÆÄæ¦ìªºÃöÁp©Ê¨Ó¨Ï¸ê®Æ®wªº¸ê®Æ§ó²Å¦X¹ê»Ú¥@¬Éªºª¬ªp¡A¦¹ºØ¨ã¦³ÃöÁp©Êªº¸ê®Æ®w¡A§YºÙ¬°ÃöÁp©Ê¸ê®Æ®w¡]Relational Databases¡^¡C

§Ú­Ì­º¥ý¥H asp/example/database/song01.mdb ¤ºªº song ¸ê®Æªí¨Ó»¡©ú¡A¨ä¤º®e¦p¤U¡G

§Ç¸¹ ºq¦±¦WºÙ ¥D°ÛªÌ ¦~¥÷
¥Î¤ß¨}­W ±i¦t 1993 
Å¥®ü ±i´f©f 1998 
²o¤â ±i´f©f 2001 
³Ì¼ô±xªº­¯¥Í¤H ¿½¨È°a 2000 
ÅʤH¥¼º¡ S.H.E 2002 
I.O.I.O. S.H.E 2002 
11 ¨C¦¸³£·Q©I³Û§Aªº¦W¦r ¥Ã¨¹ 2002 
12 ³Ì¼ô±xªº­¯¥Í¤H ¿½¨È°a 2000 
13 ­ì¨Ó§A¤°»ò³£¤£­n ±i´f©f 1999 
14 µ¡¥~ªº¤Ñ®ð ¿½¨È°a 1999 
16 ³¥¦Ê¦X¤]¦³¬K¤Ñ ¥Ã¨¹ 2002 

«Ü©úÅã¦a¡A¡u¥D°ÛªÌ¡vªºÄæ¦ì¤¤¡A«Ü¦h¸ê®Æ³£¬O­«½Æªº¡A¦pªG§Ú­ÌÁÙ­n¼W¥[©M¡u¥D°ÛªÌ¡v¬ÛÃöªº¸ê®Æ¡A¨Ò¦p¡u¥X¹D¦~¥÷¡v¡B¡u¨­°ª¡v¡B¡u¶Ý¦n¡vµ¥¡A´N·|¼W¥[§ó¦h­«ÂЪº¸ê®Æ¡A¦]¦¹¡A§Ú­Ì¥i¥H±N©M¥D°ÛªÌ¬ÛÃöªº¸ê®Æ©ñ¨ì¥t¤@­Ó¸ê®Æªí singer¡A¨Ã«ü©w¨C­ººq¦±¡]¦b song ¸ê®Æªí¡^©Ò¹ïÀ³ªººq¤â¡]¦b singer ¸ê®Æªí¡^¡A³o¼Ë°µªº¦n³B¬O¡G

¬°¤è«K»¡©ú°_¨£¡A§Ú­Ì¨Ï¥Î¤@­Ó¸ê®Æ¸û¤ÖªºÀÉ®× asp/example/database/song02.mdb ¨Ó¶i¦æ¥H¤U»¡©ú¡A¨ä¤¤ song ¸ê®Æªíªº¤º®e¦p¤U¡G

§Ç¸¹ ¦WºÙ ºq¤â§Ç¸¹ ¦~¥÷
¥Î¤ß¨}­W 1993 
Å¥®ü 1998 
ÅʤH¥¼º¡ 2002 
I.O.I.O. 2002 
17 ¯«¸Ü 1983 
18 ªá¤ß 1993 

singer ¸ê®Æªíªº¤º®e¦p¤U¡G

§Ç¸¹ ©m¦W Ãþ§O ¥X¹D¦~¥÷
±i¦t ¨kºq¤â 1973 
±i´f©f ¤kºq¤â 1975 
¿½¨È°a ¤kºq¤â 1971 
S.H.E ¤k¹ÎÅé 1960 
11 ¥Ã¨¹ ¨kºq¤â 1969 

¥Ñ¤W­z¦Cªí¥i¥Hª¾¹D¡A¨C¤@­ººq¦³¤@­ÓÄæ¦ì¬O¡uºq¤â§Ç¸¹¡v¡A«ü¨ì singer ¸ê®Æªíªº¡u§Ç¸¹¡vÄæ¦ì¡A´N¥i¥H§ä¨ì¹ïÀ³ªººq¤â¸ê®Æ¡C§Ú­Ì¥i¥H¨Ï¥Î SQL ¬d¸ß¨Ó¦C¥Xºq¤â©Mºq¦±ªº©Ò¦³¥i¯à²Õ¦X¡A©Ò¥Îªº SQL «ü¥O¬°¡G

SELECT song.¦WºÙ, singer.©m¦W FROM song, singer; ©Ò±o¨ìªºµ²ªG¬O¡G

¦WºÙ ©m¦W
¥Î¤ß¨}­W ±i¦t 
Å¥®ü ±i¦t 
ÅʤH¥¼º¡ ±i¦t 
I.O.I.O. ±i¦t 
¯«¸Ü ±i¦t 
ªá¤ß ±i¦t 
¥Î¤ß¨}­W ±i´f©f 
Å¥®ü ±i´f©f 
ÅʤH¥¼º¡ ±i´f©f 
I.O.I.O. ±i´f©f 
¯«¸Ü ±i´f©f 
ªá¤ß ±i´f©f 
¥Î¤ß¨}­W ¿½¨È°a 
Å¥®ü ¿½¨È°a 
ÅʤH¥¼º¡ ¿½¨È°a 
I.O.I.O. ¿½¨È°a 
¯«¸Ü ¿½¨È°a 
ªá¤ß ¿½¨È°a 
¥Î¤ß¨}­W S.H.E 
Å¥®ü S.H.E 
ÅʤH¥¼º¡ S.H.E 
I.O.I.O. S.H.E 
¯«¸Ü S.H.E 
ªá¤ß S.H.E 
¥Î¤ß¨}­W ¥Ã¨¹ 
Å¥®ü ¥Ã¨¹ 
ÅʤH¥¼º¡ ¥Ã¨¹ 
I.O.I.O. ¥Ã¨¹ 
¯«¸Ü ¥Ã¨¹ 
ªá¤ß ¥Ã¨¹ 

³o­Ó¦Cªí¦@¦³ 30 µ§¸ê®Æ¡A¥Nªí¥Ñ²Ä¤@­Ó¸ê®Æªí¡]6 µ§¸ê®Æ¡^©M²Ä¤G­Ó¸ê®Æªí¡]5 µ§¸ê®Æ¡^ªº©Ò¦³¥i¯à²Õ¦X¡A¦ý¬O§Ú­Ì¨Ã¨S¦³¥Î¨ì¨â­Ó¸ê®Æªí¤§¶¡ªºÃöÁp¡A©Ò¥H±o¨ìªºµ²ªG·íµM¤£¥¿½T¡C§Ú­Ì¥i¥H§â¦³ÃöÁp©ÊªºÄæ¦ì¡]¦b¦¹¨Ò¬O song ¸ê®Æªíªº¡uºq¤â§Ç¸¹¡v¥H¤Î singer ¸ê®Æªíªº¡u§Ç¸¹¡vÄæ¦ì¡^¥[¤J±ø¥ó¦¡¡A±o¨ì¤U¦Cªº SQL «ü¥O¡G

SELECT song.¦WºÙ, singer.©m¦W FROM singer, song WHERE (song.ºq¤â§Ç¸¹=singer.§Ç¸¹); ¹ïÀ³ªº¡u³]­pÀ˵ø¡vµe­±¬O¡G

©Ò±o¨ìªºµ²ªG¬O¡G

¦WºÙ ©m¦W
¥Î¤ß¨}­W ±i¦t 
Å¥®ü ±i´f©f 
ÅʤH¥¼º¡ S.H.E 
I.O.I.O. S.H.E 

¤W­zªº°µªk¬O¥H where ¤l¥y¨Ó«ü©w¸ê®Æ©Ò¥²¶·¨ã³Æªº¯S©Ê¡A¥t¤@ºØ°µªk«h¬Oª½±µ¦b¡u³]­pÀ˵ø¡v¤¤«ü©w¸ê®Æªí¤§¶¡ªºÃöÁp¡A§Ú­Ì¥i¥ý±N song ©M singer ¸ê®Æªí¥[¤J¡u³]­pÀ˵ø¡vªº¬d¸ß¡AµM«á¿ï¨ú song ¸ê®Æªíªº¡uºq¤â§Ç¸¹¡vÄæ¦ì«á¡A©ì©ñ¨ì singer ¸ê®Æªíªº¡u§Ç¸¹¡vÄæ¦ì¡A´N¥i¥H«Ø¥ß³o¨â­Ó¸ê®ÆªíªºÃöÁp©Ê¡]·|²£¥ÍÃöÁp½u¬q¥H³s±µ¦³ÃöÁp©ÊªºÄæ¦ì¡^¡AµM«á¦A¥[¤J»Ý­n¬d¸ßªºÄæ¦ì¦WºÙ¡AÅã¥Üµe­±¦p¤U¡G

¹ïÀ³ªº SQL «ü¥O¦p¤U¡G SELECT song.§Ç¸¹, song.¦WºÙ, singer.©m¦W FROM song INNER JOIN singer ON song.ºq¤â§Ç¸¹ = singer.§Ç¸¹; ©Ò±o¨ìªºµ²ªG¬O¡G

§Ç¸¹ ¦WºÙ ©m¦W
¥Î¤ß¨}­W ±i¦t 
Å¥®ü ±i´f©f 
ÅʤH¥¼º¡ S.H.E 
I.O.I.O. S.H.E 

¦b¤W­z½d¨Ò¤¤¡ASQL «ü¥O¨Ï¥Î¤F INNER JOIN¡C¨Æ¹ê¤W¡A¹ï©ó¸ê®Æªíªº³s±µ¡]Join¡^¡A¥i¥H¤À¦¨¤TÃþ¡G

  1. Inner Join¡G¥u·|¦C¥X¨Ó¨â­Ó¸ê®Æªí³s±µÄæ¦ìªº¸ê®Æ¬Û¦Pªº°O¿ý¡C
  2. Left Join¡G¦C¥X©Ò¦³¨Ó¦Û¥ª¸ê®Æªíªº°O¿ý¡A¥H¤Î³s±µÄæ¦ì¬Ûµ¥ªº¥k¸ê®Æªíªº°O¿ý
  3. Right Join¡G¦C¥X©Ò¦³¨Ó¦Û¥k¸ê®Æªíªº°O¿ý¡A¥H¤Î³s±µÄæ¦ì¬Ûµ¥ªº¥ª¸ê®Æªíªº°O¿ý
¤å¦r»¡©ú¨Æ¹ê¤W¤£®e©öÁA¸Ñ¡AÅý§Ú­Ì¨Ó¬Ý¬Ý½d¨Ò¡C¦pªG§Ú­ÌÂùÀ»³s±µÃöÁp©ÊÄæ¦ìªº½u¬q¡A´N·|¸õ¥X¡u³s±µÄÝ©Ê¡vªºµøµ¡¡A¦p¤U¡G

¦¹µøµ¡ªº¹w³]­È´N¬O¡u¶È¥]§t¨â­Ó¸ê®Æªí³s±µÄæ¦ìªº¸ê®Æ¬Û¦Pªº°O¿ý¡v¡A³o´N¬O Inner Join¡C¦pªG§Ú­ÌÂI¿ï²Ä 2­Ó¿ï¶µ¡]¡u¥]¬A©Ò¦³¨Ó¦Û'song'ªº°O¿ý©M¥u¥]¬A¨º¨Ç³s±µÄæ¦ì¬Ûµ¥ªº'singer'Äæ¦ì¡v¡^¡A³o´N¬O Left Join¡A«ö¡u½T©w¡v¤§«á¡A¬d¸ßµe­±¦p¤U¡G

¨ä¤¤ÃöÁp½u¬q³Q¥[¤W¤F¤@­Ó¥Ñ¥ª¦V¥kªº½bÀY¡A¥Nªí Left Join¡A¬Û¹ïÀ³ªº SQL ©R¥O¦p¤U¡G ¹ïÀ³ªº SQL «ü¥O¦p¤U¡G SELECT song.§Ç¸¹, song.¦WºÙ, singer.©m¦W FROM song LEFT JOIN singer ON song.ºq¤â§Ç¸¹ = singer.§Ç¸¹; ©Ò±o¨ìªºµ²ªG¬O¡G

§Ç¸¹ ¦WºÙ ©m¦W
¥Î¤ß¨}­W ±i¦t 
Å¥®ü ±i´f©f 
ÅʤH¥¼º¡ S.H.E 
I.O.I.O. S.H.E 
17 ¯«¸Ü null 
18 ªá¤ß null 

¦¹µ²ªG¥Nªí¡u¦C¥X©Ò¦³ªººq¡A¥H¤Î³o¨Çºq©Ò¥i¯à¹ïÀ³ªººq¤â¡v¡C

Hint
¡unull¡v¥Nªí¨S¦³¸ê®Æ¦s¦b¡C

¦pªG§Ú­ÌÂI¿ï²Ä 3 ­Ó¿ï¶µ¡]¡u¥]¬A©Ò¦³¨Ó¦Û'singer'ªº°O¿ý©M¥u¥]¬A¨º¨Ç³s±µÄæ¦ì¬Ûµ¥ªº'song'Äæ¦ì¡v¡^¡A³o´N¬O Right Join¡A¹ïÀ³ªº SQL ©R¥O¦p¤U¡G

SELECT song.§Ç¸¹, song.¦WºÙ, singer.©m¦W FROM song RIGHT JOIN singer ON song.ºq¤â§Ç¸¹ = singer.§Ç¸¹; ©Ò±o¨ìªºµ²ªG¬O¡G

§Ç¸¹ ¦WºÙ ©m¦W
¥Î¤ß¨}­W ±i¦t 
Å¥®ü ±i´f©f 
null null ¿½¨È°a 
I.O.I.O. S.H.E 
ÅʤH¥¼º¡ S.H.E 
null null ¥Ã¨¹ 

¦¹µ²ªG¥Nªí¡u¦C¥X©Ò¦³ªººq¤â¡A¥H¤Î³o¨Çºq¤â©Ò¥i¯à¹ïÀ³ªººq¡v¡C

Hint
¦³Ãö©ó Outer Join¡A»¡©ú¦p¤U¡G
  • Left Join ¤SºÙ¬° Left Outer Join¡FRight Join ¤SºÙ¬° Right Outer Join¡C
  • Full Outer Join ´N¬O Left Join ©M Right Join ªºÁp¶°¡A¦ý¬O Access ¥Ø«e¨Ã¤£¤ä´©¡C

¥H¤Wªº°µªk¡A¬O¦b«Ø¥ß¬d¸ß®É¡A¤~«Ø¥ß°_¸ê®Æªí¤§¶¡ªºÃöÁp¡A¥t¤@­Ó¤è¦¡¡A«h¬O¨Æ¥ý´N«Ø¥ß¦n¸ê®ÆªíªºÃöÁp¡A³o¬O¥Ã¤[©ÊªºÃöÁp¡A©Ò¥H¦b¶i¦æ¬d¸ß®É¡A¤]·|±N¦¹ÃöÁp¥]§t¶i¨Ó¡C¥H¸ê®Æ®w song02.mdb ¬°¨Ò¡A§Ú­Ì¥i¥H¶}±Ò¡u¸ê®Æ®w¤u¨ã/¸ê®Æ®wÃöÁp¹Ï¡v¡A¬Ý¨ìªºµe­±¦p¤U¡G

§Ú­Ì¥i¥H¿ï¨ú song ¸ê®Æªíªº¡uºq¤â§Ç¸¹¡vÄæ¦ì«á¡A©ì©ñ¨ì singer ¸ê®Æªíªº¡u§Ç¸¹¡vÄæ¦ì¡A´N¥i¥H«Ø¥ß³o¨â­Ó¸ê®Æªíªº¥Ã¤[ÃöÁp¡A¦¹®É·|¸õ¥X¨Ó¡u½s¿èÃöÁp¡vªºµe­±¡A¦p¤U¡G

¦³Ãö³o­Óµe­±ªº¿ï¶µ¡u±j­¢°Ñ¦Ò§¹¾ã©Ê¡v¡A§Ú­Ì·|¦b¤U¤@¸`»¡©ú¡C¥Ø«e§A¥u­n«ö¤U¡u«Ø¥ß¡v¡A§Y¥i«Ø¥ß®Ú°ò©ó Inner Join ªº¥Ã¤[ÃöÁp¡A§A¤]¥i¥H«ö¤U¡u³s±µÃþ«¬¡v¡A¨Ó³]©w¨ä¥LÃþ§Oªº Join¡A¦p Left Join ©Î¬O Right Join¡C¦¹·s«Ø¥ßªºÃöÁp¡A·|¦b¡u¸ê®Æ®wÃöÁp¹Ï¡v¥H½u¬qªº¤è¦¡Åã¥Ü¥X¨Ó¡A¦¹½u¬q©Ò³sµ²ªºÄæ¦ì§Y¬O¤¬¦³ÃöÁpªºÄæ¦ì¡Aµe­±¦p¤U¡G

­Y­n½s¿èÃöÁp¡A¥i¥HÂùÀ»¦¹½u¬q¡A§Y¥i¶}±Ò¡u½s¿èÃöÁp¡vªºµøµ¡¡C

¤@¥¹«Ø¥ß¥Ã¤[ÃöÁp«á¡A¦b¡u³]­pÀ˵ø¡v¼Ò¦¡¤U½s¿è¬d¸ß®É¡A¥u­n¥[¤J¬ÛÃöªº¸ê®Æªí¡A§Y¥i¤ÏÀ³¦¹¥Ã¤[ÃöÁp¡A¦Ó¤£»Ý­n¦A¦Û¦æ¤â°Ê¥[¤J¡C


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò